package com.zuoyy.admin.system.controller;

import com.zuoyy.admin.common.BaseController;
import com.zuoyy.admin.common.component.actionLog.action.StatusAction;
import com.zuoyy.admin.common.component.actionLog.annotation.ActionLog;
import com.zuoyy.admin.common.component.actionLog.annotation.EntityParam;
import com.zuoyy.admin.system.service.PostInterface;
import com.zuoyy.common.enums.StatusEnum;
import com.zuoyy.common.utils.StatusUtil;
import com.zuoyy.common.vo.JsonResponse;
import com.zuoyy.modules.system.domain.Post;
import com.zuoyy.modules.system.query.PostQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 
 */
@Controller
@RequestMapping("/system/post")
public class PostController extends BaseController {

    //页面地址
    private static final String PAGE_PATH = "/system/post";
    //权限标示
    private static final String PERMISSION_FLAG = "system:post:";


    @Autowired
    private PostInterface postInterface;


    @GetMapping("/index.shtml")
    @RequiresPermissions(PERMISSION_FLAG+"index")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("res", getMyResources(request,response));
        return PAGE_PATH+"/index";
    }

    @GetMapping("/list/page")
    @RequiresPermissions(PERMISSION_FLAG+"list")
    public String listPage(Model model) {
        model.addAttribute("button", getPageButton(PERMISSION_FLAG+"list"));
        return PAGE_PATH+"/list";
    }

    @GetMapping("/add/page")
    @RequiresPermissions(PERMISSION_FLAG+"add")
    public String toAdd() {
        return PAGE_PATH+"/add";
    }

    @GetMapping("/edit/page/{id}")
    @RequiresPermissions(PERMISSION_FLAG+"edit")
    public String toEdit(@PathVariable("id") Post domain, Model model) {
        model.addAttribute("domain", domain);
        return PAGE_PATH+"/add";
    }



    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public JsonResponse getListPage(PostQuery query) {
        return postInterface.getListPage(query);
    }

    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({PERMISSION_FLAG+"add", PERMISSION_FLAG+"edit"})
    public JsonResponse save(@EntityParam @RequestBody Post domain) {
        return postInterface.saveOrUpdate(domain);
    }

    @ResponseBody
    @RequestMapping("/status/{param}")
    @RequiresPermissions({PERMISSION_FLAG+"status",PERMISSION_FLAG+"delete"})
    @ActionLog(name = "状态", action = StatusAction.class)
    public JsonResponse status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<String> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        return postInterface.updateStatus(statusEnum,ids);
    }

}